package com.itheima.health.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    String cacheKey = "health-setmeal";

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
        //删除缓存
        redisTemplate.delete(cacheKey);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //读取缓存
        String setmealsJson = redisTemplate.opsForValue().get(cacheKey);
        if (setmealsJson != null && !"".equals(setmealsJson)) {
            return JSON.parseObject(setmealsJson, PageResult.class);
        }
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //查询完存入缓存
        redisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(new PageResult(page.getTotal(), page.getResult())));
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result findById(Integer id) {
        Setmeal setmeal = setMealDao.selectById(id);
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmeal);
    }

    /**
     * 编辑
     *
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result edit(Setmeal setmeal, List<Integer> checkGroupIds) {
        //判断非空
        if (checkGroupIds == null || checkGroupIds.size() == 0) {
            throw new BusinessRuntimeException(MessageConst.ACTION_FAIL);
        }
        //删除套餐-检查组关联
        setMealDao.delectSetmealCheckgroupBySetmealId(setmeal.getId());
        //新增套餐-检查组关联
        for (Integer checkgroupId : checkGroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(), checkgroupId);
        }
        //编辑套餐
        setMealDao.update(setmeal);
        //删除缓存
        redisTemplate.delete(cacheKey);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteSetmealById(Integer id) {
        //删除套餐-检查组
        setMealDao.delectSetmealCheckgroupBySetmealId(id);
        //删除套餐
        setMealDao.delectById(id);
        //删除缓存
        redisTemplate.delete(cacheKey);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    /**
     * 体检预约-全部套餐查询
     * @return
     */
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public Result getSetmeal() {
        Page<Setmeal> setmeals = setMealDao.selectByCondition(null);
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmeals.getResult());
    }

    /**
     * C端-查询套餐详情
     * @param id
     * @return
     */
    @Override
    public Result userFindById(Integer id) {
        Setmeal setmeal = setMealDao.selectById(id);
        //根据套餐id查询检查组
        List<CheckGroup> checkGroupList = setMealDao.selectCheckGroupBySetmealId(id);
        //根据检查组id查询套餐
        for (CheckGroup checkGroup : checkGroupList) {
            List<CheckItem> checkItemList = new ArrayList<>();
            List<Integer> checkItemIds = checkGroupDao.selectCheckItemIdsByCheckGroupId(checkGroup.getId());
            for (Integer checkItemId : checkItemIds) {
                CheckItem checkItem = checkItemDao.selectById(checkItemId);
                checkItemList.add(checkItem);
            }
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroupList);
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmeal);
    }
}
